package com.axter.libs.photo.widget;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;
import android.widget.Toast;

import com.axter.libs.photo.R;

import java.util.ArrayList;
import java.util.List;

/**
 * 底部弹出菜单按钮对话框
 *
 * @author axter
 */
public class AutoBottomMenuDialog extends Dialog {
    public static class MenuItem {
        /**
         * 文本框
         */
        public static final int MENUTYPE_TEXT = 1;
        /**
         * 按钮
         */
        public static final int MENUTYPE_BUTTON = 2;
        /**
         * 分割线
         */
        public static final int MENUTYPE_LINE = 3;
        /**
         * 留白
         */
        public static final int MENUTYPE_SPACE = 4;
        /**
         * 自定义
         */
        public static final int MENUTYPE_CUSTOM = 5;
        public int menu_type;
        public Object value;
        public int textSize;
        public Object textColor;
        public Object backgroupResource;
        public Object backgroupColor;

        public MenuItem(int menu_type, Object value) {
            this.menu_type = menu_type;
            this.value = value;
        }
    }

    private Context mContext;
    private List<MenuItem> elements = new ArrayList<>();
    private View.OnClickListener listener;
    /**
     * 自动添加分割线
     */
    private boolean isAutoLine;
    /**
     * 是否背景变暗
     */
    private boolean isDimEnabled;

    private LinearLayout menuContainer;
    private LayoutParams menuParams;
    private LayoutParams buttonParams;
    private LayoutParams lineParams;
    private LayoutParams spaceParams;

    public AutoBottomMenuDialog(Context context) {
        this(context, true, true, true);
    }

    public AutoBottomMenuDialog(Context context, boolean isAutoLine, boolean isCanceledOuutside, boolean isDimEnabled) {
        super(context, R.style.axter_bottom_menu_dialog);
        mContext = context;
        this.isAutoLine = isAutoLine;
        this.isDimEnabled = isDimEnabled;
        setCanceledOnTouchOutside(isCanceledOuutside);
        if (isDimEnabled) {
            getWindow().getAttributes().dimAmount = 0.6f;
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initButtonsMenu();
    }

    @Override
    public void show() {
        if (elements == null || elements.size() == 0) {
            Toast.makeText(mContext, R.string.photoselect_nothing_show, Toast.LENGTH_SHORT).show();
            return;
        }
        super.show();
    }

    private int spaceHeight = 15;
    private int lineHeight = 1;
    private int containerColor = 0xFFF0F0F0;
    private int textSize = 18;
    private int textPadding = 15;
    private int textBackGroupResource = android.R.color.white;
    private int buttonBackGroupResource = android.R.color.white;
    private int textColor = 0xff999999;
    private int buttonColor = 0xff2B2B2B;
    private int lineColor = 0x80cfcfcf;

    private int mScreenWidth;

    /**
     * 初始化底部菜单
     */
    private void initButtonsMenu() {
        DisplayMetrics dm = getContext().getResources().getDisplayMetrics();
        lineHeight = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, lineHeight, dm);
        spaceHeight = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, spaceHeight, dm);
        textPadding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, textPadding, dm);

        mScreenWidth = getContext().getResources().getDisplayMetrics().widthPixels;
        menuParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        buttonParams = new LayoutParams(mScreenWidth, LayoutParams.WRAP_CONTENT);
        buttonParams.gravity = Gravity.CENTER;
        lineParams = new LayoutParams(mScreenWidth, lineHeight);
        spaceParams = new LayoutParams(1, spaceHeight);

        menuContainer = new LinearLayout(mContext);
        menuContainer.setOrientation(LinearLayout.VERTICAL);
        menuContainer.setLayoutParams(menuParams);
        menuContainer.setBackgroundColor(containerColor);

        int id = 0;
        for (int i = 0, len = elements.size(); i < len; i++) {
            MenuItem item = elements.get(i);
            if (isAutoLine && i == 0) {
                menuContainer.addView(getLineView(null));
            }
            switch (item.menu_type) {
                case MenuItem.MENUTYPE_BUTTON:
                    Button button = getButtonView(item);
                    menuContainer.addView(button);
                    button.setId(id++);
                    if (listener != null) {
                        button.setOnClickListener(listener);
                    }
                    break;
                case MenuItem.MENUTYPE_TEXT:
                    TextView textView = getTextView();
                    textView.setText(item.value.toString());
                    menuContainer.addView(textView);
                    break;
                case MenuItem.MENUTYPE_LINE:
                    menuContainer.addView(getLineView(item));
                    break;
                case MenuItem.MENUTYPE_SPACE:
                    menuContainer.addView(getSpaceView());
                    break;
                case MenuItem.MENUTYPE_CUSTOM:
                    if (item.value != null && item.value instanceof View) {
                        menuContainer.addView((View) item.value);
                    }
                    break;
            }
            if (isAutoLine) {
                menuContainer.addView(getLineView(null));
            }
        }
        setContentView(menuContainer);
        getWindow().setGravity(Gravity.BOTTOM);
        getWindow().setWindowAnimations(R.style.axter_bottom_menu_ani);
    }

    private TextView getTextView() {
        TextView textView = new TextView(mContext);
        textView.setLayoutParams(buttonParams);
        textView.setGravity(Gravity.CENTER);
        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, textSize);
        textView.setTextColor(textColor);
        textView.setPadding(0, textPadding, 0, textPadding);
        textView.setBackgroundResource(textBackGroupResource);

        return textView;
    }

    private Button getButtonView(MenuItem item) {
        Button button = new Button(mContext);
        button.setLayoutParams(buttonParams);
        button.setTextSize(TypedValue.COMPLEX_UNIT_SP, textSize);
        button.setTextColor(buttonColor);
        button.setPadding(0, textPadding, 0, textPadding);
        button.setBackgroundResource(buttonBackGroupResource);
        if (item.value != null) {
            if (item.value instanceof Integer) {
                button.setText((int) item.value);
            } else if (item.value instanceof String) {
                button.setText(item.value.toString());
            }
        }
        if (item.textSize != 0) {
            button.setTextSize(TypedValue.COMPLEX_UNIT_SP, item.textSize);
        }
        if (item.textColor != null && item.textColor instanceof Integer) {
            button.setTextColor((Integer) item.textColor);
        }
        if (item.backgroupResource != null && item.backgroupResource instanceof Integer) {
            button.setBackgroundResource((Integer) item.backgroupResource);
        } else if (item.backgroupColor != null && item.backgroupColor instanceof Integer) {
            button.setBackgroundColor((Integer) item.backgroupColor);
        }
        return button;
    }

    private View getLineView(MenuItem item) {
        View line = new View(mContext);
        line.setLayoutParams(lineParams);
        if (item != null && item.value != null && item.value instanceof Integer) {
            line.setBackgroundColor((Integer) item.value);
        } else {
            line.setBackgroundColor(lineColor);
        }
        return line;
    }

    private View getSpaceView() {
        View space = new View(mContext);
        space.setLayoutParams(spaceParams);
        return space;
    }

    public void addCustomView(View view) {
        elements.add(new MenuItem(MenuItem.MENUTYPE_CUSTOM, view));
    }

    public void addLineView(Object color) {
        elements.add(new MenuItem(MenuItem.MENUTYPE_LINE, color));
    }

    public void addButtonView(String txt) {
        elements.add(new MenuItem(MenuItem.MENUTYPE_BUTTON, txt));
    }

    public void addSpaceView() {
        elements.add(new MenuItem(MenuItem.MENUTYPE_SPACE, null));
    }

    public void addTextView(String txt) {
        elements.add(new MenuItem(MenuItem.MENUTYPE_TEXT, txt));
    }

    public void addViewByMenuItem(MenuItem menuItem) {
        elements.add(menuItem);
    }

    public void setOnclickListener(View.OnClickListener listener) {
        this.listener = listener;
    }

    private static AutoBottomMenuDialog dialog;

//	public static void test(final Context context) {
//		if (dialog == null) {
//			dialog = new AutoBottomMenuDialog(context);
//			dialog.addTextView("我是标题头,你敢信!");
//			dialog.addSpaceView();
//			dialog.addButtonView("输入线路");
//			dialog.addButtonView("选择运动记录作为线路");
//			dialog.addSpaceView();
//			MenuItem cancel = new MenuItem(MenuItem.MENUTYPE_BUTTON, "取消");
//			cancel.textColor = 0xffEF5D06;
//			dialog.addViewByMenuItem(cancel);
//			dialog.setOnclickListener(new View.OnClickListener() {
//				@Override
//				public void onClick(View v) {
//					Toast.makeText(context, ((Button) v).getText() + "|" + v.getId(), Toast.LENGTH_SHORT).show();
//					switch (v.getId()) {
//						case 0:
//						case 1:
//							break;
//						case 2:
//							dialog.dismiss();
//							break;
//					}
//				}
//			});
//		}
//		dialog.show();
//	}

    public static void test(final Context context) {
        if (dialog == null) {
            dialog = new AutoBottomMenuDialog(context, false, true, true);
            dialog.addButtonView(context.getString(R.string.photoselect_shot));
            dialog.addLineView(Color.TRANSPARENT);
            dialog.addButtonView(context.getString(R.string.photoselect_select_from_album));
            dialog.addSpaceView();
            dialog.addButtonView(context.getString(R.string.photoselect_cancel));
            dialog.setOnclickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(context, ((Button) v).getText() + "|" + v.getId(), Toast.LENGTH_SHORT).show();
                    switch (v.getId()) {
                        case 0:
                        case 1:
                            break;
                        case 2:
                            dialog.dismiss();
                            break;
                    }
                }
            });
        }
        dialog.show();
    }
}